package com.example.cloud.service.business.medical.impl;

import com.example.cloud.dao.business.medical.TreatPlanCourseRecordRepository;
import com.example.cloud.model.business.medical.TreatPlanCourseRecord;
import com.example.cloud.service.business.medical.TreatPlanCourseRecordService;
import com.example.cloud.utils.BusinessUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by wangtao on 2018/6/6.
 */
@Service
public class TreatPlanCourseRecordServiceImpl implements TreatPlanCourseRecordService {

    private static final Logger logger = Logger.getLogger(TreatPlanCourseRecordServiceImpl.class);

    @PersistenceContext
    EntityManager em;

    @Autowired
    private TreatPlanCourseRecordRepository treatPlanCourseRecordRepository;

    @Override
    @Transactional
    public TreatPlanCourseRecord doSave(TreatPlanCourseRecord record) {
        record.setCreationTime(null);
        BusinessUtil.appendRecord(record);
        return treatPlanCourseRecordRepository.save(record);
    }

    @Override
    @Transactional
    public void doDelete(List<Long> ids) {
        // 逻辑删除
        StringBuilder sql = new StringBuilder("update treat_plan_course_record set delete_flag = 1 where id in(:ids)");
        Query query = em.createNativeQuery(sql.toString());
        query.setParameter("ids", ids);
        query.executeUpdate();
    }
}
